package com.drojian.page4;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.drojian.demo.R;
import java.util.ArrayList;
import java.util.List;


public class BlankFragment1 extends Fragment {
    private  List<Data> dataList = new ArrayList<Data>();

    private View rootview;
    TextView idTextView;
    TextView nameTextView;
    private Button selectAllButton;
    private Button deleteSelectedButton;
    private boolean isSelectAllChecked = false;// 默认为不选

    public BlankFragment1() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(rootview == null){
            rootview =  inflater.inflate(R.layout.fragment_blank1,container,false);
        }
        // 获取 RecyclerView 组件，设置布局管理器，并初始化适配器
        RecyclerView recyclerView = rootview.findViewById(R.id.recyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(requireContext());
        recyclerView.setLayoutManager(layoutManager);
        DataAdapter adapter = new DataAdapter(dataList);
        recyclerView.setAdapter(adapter);

        idTextView = rootview.findViewById(R.id.id);
        nameTextView =rootview.findViewById(R.id.name);
        selectAllButton = rootview.findViewById(R.id.select_all_button);
        deleteSelectedButton = rootview.findViewById(R.id.delete_selected_button);

        selectAllButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isSelectAllChecked = !isSelectAllChecked; // 反转全选状态
                // 遍历数据列表，更新所有数据项的选中状态
                for (Data item : dataList) {
                    item.setSelected(isSelectAllChecked);
                }
                adapter.notifyDataSetChanged();
                updateDeleteSelectedButtonState();
            }
        });

        deleteSelectedButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Data> delete = new ArrayList<>();
                for (int i = 0; i < dataList.size(); i++) {
                    if (dataList.get(i).isSelected()) {
                        delete.add(dataList.get(i));
                    }
                }
                for (Data item : delete) {
                    dataList.remove(item);
                    adapter.notifyItemRemoved(dataList.indexOf(item));
                }
                updateDeleteSelectedButtonState();
                Toast.makeText(getContext(), "已删除所有选中项", Toast.LENGTH_SHORT).show();

            }
        });
        // 初始化删除选中项按钮状态
        updateDeleteSelectedButtonState();
        return rootview;
    }


    //删除所有按钮是否可以被点击
    private void updateDeleteSelectedButtonState() {
        deleteSelectedButton.setEnabled(selected());
    }

    //判断是否被勾选
    private boolean selected() {
        for (Data item : dataList) {
            if (item.isSelected()) {
                return true;
            }
        }
        return false;
    }


    public void initData(){
        for(int i = 1; i <= 100; i++){
            Data data1 = new Data();
            data1.setId(i);
            data1.setName("     我是第"+i+"条数据");
            dataList.add(data1);
        }
    }
}